﻿/*
*   公开课  
*/
angular.module('jisuStuHome').controller('publicClassController', ['$scope', '$rootScope', '$sce', '$translate', '$location', 'sysUserInfo', 'T', 'RequestTrainService', 'StuRequestTrain', 'Msg', function ($scope, $rootScope, $sce, $translate, $location, sysUserInfo, T, RTS, ST, Msg) {
    $scope.userInfo = sysUserInfo.getuserinfo();
    $scope.orgname = sysUserInfo.getuserinfo().organization_Name;
    $scope.token = sysUserInfo.getuserinfotoken();
    // 筛选 （综合排序、学习人数、最新发布）
    $scope.paramsScreen = [
        { name: T.T("LatestRelease"), flag: true, value: 1 },
        { name: T.T("NumberOfLearners"), flag: false, value: 2 },
        { name: T.T("ComprehensiveRanking"), flag: false, value: 3 }
    ];
    // 按照类型筛选（录播、直播、文档、试卷、线下）
    // 1.视频 2.文档 3.考试(测试) 4.线下 5.题库 6.直播,8.图文
    $scope.paramsScreenType = [
        { name: T.T("Video"), flag: false, value: 1 },
        { name: T.T("File"), flag: false, value: 2 },
        { name: T.T("Paper"), flag: false, value: 3 },
        { name: T.T("LiveBroadcast"), flag: false, value: 6 },
        { name: T.T("ImageText"), flag: false, value: 8 }
    ]
    // 公共的loading
    $scope.CommonLoading = false;
    /******** 分类查询 *******/
    // 请求参数
    // 初始化
    $scope.init = function () {

        $scope.knowledgeList = [];   //知识结构列表
        $scope.currentKnowledgeObj = { knowledge_Id: "", knowledge_Name: T.T('AllCategories'), parentId: 0 }; // 全部分类   
        $scope.params = {
            userId: sysUserInfo.getuserinfo().user_ID,
            startDate: "",    // 起始时间
            endDate: "", // 结束时间
            searchName: "", //搜索内容 
            knowledge_Id: "", // 子集搜索
            orgId: sysUserInfo.getuserinfo().organization_ID,   // 企业id
            org_Name: sysUserInfo.getuserinfo().organization_Name,   // 企业id
            powerLV: 2 //登录人的系统角色         
        };
        $scope.currentKnowledgeList = {}; // 当前
        $scope.getKnowledgeList(true);
    }

    // 获取知识结构列表
    $scope.getKnowledgeList = function (flag) {
        $scope.CommonLoading = true;
        RTS.findKnowledgeList($scope.params).then(function (response) {
            $scope.CommonLoading = false;  //关闭loading
            if (response.errorcode == "0") {// 成功                                    
                if (flag) { // 判断是不是最后一个
                    $scope.knowledgeList.push($scope.knowledgeList[0]);
                }
                $scope.knowObj = { datas: response.datas, index: $scope.knowledgeList.length, haschildren: response.datas.length, currentKnowledgeObj: $scope.currentKnowledgeObj };
                $scope.knowledgeList[0] = $scope.knowObj;
                $scope.currentKnowledgeList = $scope.knowledgeList[0];
                $scope.publicCourseParams.knowledgeids = $scope.params.knowledge_Id;
                $scope.getPublicCourseList();
            } else {
                Msg.error("获取失败！");
            }
        }, function errorCallback() {
            $scope.CommonLoading = false;  //关闭loading          
            Msg.error("连接服务器失败！");
        });
    }

    // 查询子集中的列表 子集对象 当前索引
    $scope.openNode = function (item, index) {
        if ($scope.CommonLoading) {
            Msg.info('提示', '正在查询。。。');
            return;
        }
        if (item == null) {  // 最开始
            $scope.params.knowledge_Id = "";
            $scope.knowledgeList = [];
            // 发送请求
            $scope.getKnowledgeList(true);
        } else if ($scope.knowledgeList.length > index) { // 不为最后一个        
            // 清除后面
            $scope.knowledgeList.splice(index + 1, $scope.knowledgeList.length - index - 1);
            $scope.knowledgeList[index].currentKnowledgeObj = angular.copy(item);
            $scope.currentKnowledgeObj = angular.copy(item);
            // 处理子集参数
            $scope.params.knowledge_Id = item.knowledge_Id; // id            
            $scope.getKnowledgeList(false);
        } else {
            // 赋值给第一个对象做备用
            $scope.currentKnowledgeObj = angular.copy(item);
            // 0 要进行添加所以把0作为下一个选项
            $scope.knowledgeList[0].currentKnowledgeObj = angular.copy(item);
            // 处理子集参数
            $scope.params.knowledge_Id = item.knowledge_Id; // id            
            // 发送请求
            $scope.getKnowledgeList(true);
        }
    }

    /******** 筛选 *********/
    // 排序类型筛选
    $scope.screenOrder = function (item) {
        // 判断当前选中的
        angular.forEach($scope.paramsScreen, function (data, index) {
            if (data.name == item.name) {
                data.flag = true;
                $scope.publicCourseParams.searchType = item.value;
            } else {
                data.flag = false;
            }
        });
        $scope.getPublicCourseList();
    }
    // 小节类型筛选
    $scope.screenType = function (item) {
        if (item.flag == true) {    // 选中
            item.flag = false;

        } else {
            item.flag = true;
        }
        $scope.publicCourseParams.cstype = "";
        // 拼接筛选条件
        angular.forEach($scope.paramsScreenType, function (data, index) {
            if (data.flag) {
                $scope.publicCourseParams.cstype += data.value + ",";
            }
        });

        $scope.getPublicCourseList();


    }
    /******** 分类查询结束 *******/
    /******** 课程列表  *******/
    // 获取公开的请求参数
    $scope.publicCourseParams = {
        // 企业id
        orgid: sysUserInfo.getuserinfo().organization_ID,
        userid: sysUserInfo.getuserinfo().user_ID,
        knowledgeids: "", // 知识分类id
        searchText: "",  // 查询课程名称
        searchType: 1,   // 排序类型 1.创建时间 2.观看数量 3.收藏数量
        cstype: "", // 查询类型 1.视频 2.文档 3.考试(测试) 4.线下 5.题库 6.直播,8.图文
        pageIndex: 1,    // 当前页数
        pageSize: 15 // 单页条数
    }
    // 返回公开课的集合
    $scope.publicCourseList = [];
    // 获取公开课
    $scope.getPublicCourseList = function () {
        $scope.CommonLoading = true;
        // 发送请求
        ST.findOpen($scope.publicCourseParams).then(function (response) {
            $scope.CommonLoading = false;
            if (response.errorcode == 0) {
                $scope.publicCourseList = response.datas;
                $scope.paginationConf.totalItems = response.numCount;
                $scope.paginationConf.currentPage = response.pageIndex;
            } else {
                Msg.error("错误", "获取公开课失败！")
            }
        });
    }
    //回车
    $scope.findKeyEnter = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            $scope.getPublicCourseList();
        }
    }
    // 分页
    $scope.paginationConf = {
        currentPage: 1,
        totalItems: 0,
        itemsPerPage: 15,
        //pagesLength: 10,
        perPageOptions: [15, 20],
        //rememberPerPage: 'perPageItems',
        onChange: function () {
            if ($scope.CommonLoading) {
                //Msg.info("正在查询中！");
                //console.log(1);
                return;
            } else {
                if ((parseInt($scope.publicCourseParams.pageIndex) != parseInt($scope.paginationConf.currentPage)) || (parseInt($scope.publicCourseParams.pageSize) != parseInt($scope.paginationConf.itemsPerPage))) {
                    if ($scope.paginationConf.currentPage == 0) { $scope.paginationConf.currentPage = 1; }
                    $scope.publicCourseParams.pageIndex = $scope.paginationConf.currentPage;
                    $scope.publicCourseParams.pageSize = $scope.paginationConf.itemsPerPage;
                    $scope.getPublicCourseList();
                }
            }
        }
    }

    // 入口
    $scope.init();
    /********* 收藏课程 *******/
    $scope.conlletionParams = {
        collectionsId: "",   // 收藏id
        courseId: "",    // 课程id
        courseName: "",  // 课程名称
        userId: sysUserInfo.getuserinfo().user_ID // 用户id
    }
    // 收藏/取消
    $scope.collectionCourse = function (item) {
        if ($scope.CommonLoading) {
            Msg.info("提示", "正在收藏请等待！");
            return;
        }
        $scope.CommonLoading = true;
        $scope.conlletionParams.courseId = item.course_Id; // 课程id
        $scope.conlletionParams.courseName = item.course_Name; // 课程name
        var isColl = false;
        if (item.collections) { // 已收藏
            if ($scope.isNotCalculation(item.collections)) {
                if (item.collections.hasOwnProperty("id")) {
                    $scope.conlletionParams.collectionsId = item.collections.id;
                } else {
                    $scope.conlletionParams.collectionsId = item.collections;
                }
            } else {
                $scope.conlletionParams.collectionsId = item.collections;
            }
            isColl = false;
        } else {    //未收藏
            $scope.conlletionParams.collectionsId = "";
            isColl = true;
        }
        ST.modifyCollectionCourse($scope.conlletionParams).then(function (data) {
            $scope.CommonLoading = false;
            if (data.errorcode == "0" && isColl) {
                if ($scope.isNotCalculation(item.collectionCount)) {
                    item.collectionCount++;
                } else {
                    item.collectionCount = 1;
                }

                item.collections = data.data;
            } else if (!isColl) {
                //item.collectionCount--; // 收藏量减小
                if ($scope.isNotCalculation(item.collectionCount)) {
                    item.collectionCount--;
                } else {
                    item.collectionCount = 0;
                }
                delete item.collections;    // 删除此元素
            } else {
                Msg.error("提示", "请刷新页面重新收藏您的课程！");
            }
        });

    }

    //打开课程
    $scope.openKe = function (item) {
        var url = "";
        if ($rootScope.fileurl.indexOf("http") != -1) {
            url = $rootScope.upurl + '/dispatcher/redirect?url=' + encodeURI(encodeURI($rootScope.fileurl + "/resources/template/" + item.course_Id + ".html&arrangeId=1&token=" + $scope.token));
        } else {
            url = $rootScope.upurl + '/dispatcher/redirect?url=' + encodeURI(encodeURI(window.location.protocol + $rootScope.fileurl + "/resources/template/" + item.course_Id + ".html&arrangeId=1&token=" + $scope.token));
        }
        window.open(url);
    }

    //计算收藏数量
    $scope.isNotCalculation = function (oldcount) {
        if (oldcount != null && oldcount != undefined && oldcount != "" && oldcount != 0) {
            return true;
        } else {
            return false;
        }
    }
} ]);
